﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

using Museum.DBAccess;

public partial class ManageTours : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            BindGridData();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    /// <summary>
    /// Delete records from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        int artId = Convert.ToInt32(GridViewTours.DataKeys[e.RowIndex].Value);
        int result = 0;
        try
        {
            result = DBAccess.DeleteTourData(artId);
            Alert.ShowMessage("Record Deleted successfully.");
            // Refresh the data
            BindGridData();
        }
        catch (SqlException ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindGridData()
    {
        DataTable dtTours = new DataTable();
        try
        {
            if (Session["MuseumId"] != null)
            {
                if (Convert.ToInt32(Session["MuseumId"]) > 0)
                    dtTours = DBAccess.GetToursByMuseumId(Convert.ToInt32(Session["MuseumId"]));
            }
            else
                dtTours = DBAccess.GetAllTours();

            //assign DataTable to GridView
            GridViewTours.DataSource = dtTours;
            ViewState["gvData"] = dtTours;
            GridViewTours.DataBind();
        }
        catch
        {
        }
        finally
        {
        }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewTours.PageIndex = e.NewPageIndex;
        if (ViewState["gvData"] != null)
            GridViewTours.DataSource = (DataTable)ViewState["gvData"];
        GridViewTours.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewTours.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewTours.DataSource = dataView;
            GridViewTours.DataBind();
        }
    }
}